package com.wpw.java7demo.file;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

import org.junit.Test;

public class ReadAndWriteSameTime {

	@Test
	public void test1() throws IOException {
		final byte[] allBytes = Files.readAllBytes(Paths.get("E:/", "base.txt"));
		final String text = new String(allBytes);
		
		for (int i = 0; i < 20; i++) {
			
			new Thread(new Runnable() {
				
				@Override
				public void run() {
					try {
						byte[] readAllBytes = Files.readAllBytes(Paths.get("E:/", "new.txt"));
						String txtRead = new String(readAllBytes);
						//System.out.println(txtRead);
						if (text == null || text == "") {
							System.err.println("kong");
						}
						
						if (!text.equals(txtRead)) {
							System.err.println("buyiyang");
						}
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}).start();
			
			
			new Thread(new Runnable() {
				
				@Override
				public void run() {
					try {
						Files.write(Paths.get("E:/", "new.txt"), allBytes, StandardOpenOption.WRITE);
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}).start();
		}
	}
}
